package com.eachonline.smart.web.feekback;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartSystemInfo;
import com.eachonline.smart.model.SmartSystemInfoExample;
import com.eachonline.smart.service.SystemInfoService;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;


@Controller
@RequestMapping("/sysInfo")
public class SystemInfoController {

	@Autowired
	public SystemInfoService  systemInfoService;
	
	/**
	 * 意见反馈
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "manage/save",method = RequestMethod.POST)
	public JsonResult saveSystemInfo(HttpServletRequest request, SmartSystemInfo  systemInfo){
		
		if(StringUtil.isEmpty(systemInfo.getTitile()))
		{
			return  new JsonResult(false, "标题不能为空");
		}
		if(StringUtil.isEmpty(systemInfo.getContent()))
		{
			return  new JsonResult(false, "内容不能为空");
		}
		
		
		
		systemInfo.setSid(UUIDUtil.getUUID());
		systemInfo.setCreateTime(new Date());
		int  row =systemInfoService.insert(systemInfo);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else{
			return   new JsonResult(true, "请稍后再试");
		}
	}
	
	
	
	/**
	 * 意见反馈
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "manage/update",method = RequestMethod.POST)
	public JsonResult updateSystemInfo(HttpServletRequest request, SmartSystemInfo  systemInfo){
		
	
		systemInfo.setCreateTime(new Date());
		int  row =systemInfoService.updateByPrimaryKeySelective(systemInfo);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else{
			return   new JsonResult(true, "请稍后再试");
		}
	}
	
	/**
	 * 意见反馈列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/queryList",method = RequestMethod.POST)
	public JsonResult queryFeedBackList(HttpServletRequest request){
		

		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		SmartSystemInfoExample  fbex=new SmartSystemInfoExample();
		
		Pagination<SmartSystemInfo> systemPageInfo= systemInfoService.queryByPage(fbex, begin, pageSize," create_time desc");
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("systemPageInfo", systemPageInfo);
		return new JsonResult(true, "成功", resultMap);
	
		
	}
		
	
	
	
	/**
	 * 意见反馈详情
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/queryDetail",method = RequestMethod.POST)
	public JsonResult queryFeedBackDetail(HttpServletRequest request,String sid){
		
		SmartSystemInfo smartInfo= systemInfoService.selectByPrimaryKey(sid) ;
		if(smartInfo==null)
		{
			return new JsonResult(false, "不存在信息");
		}
		
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("smartInfo", smartInfo);
		return new JsonResult(true, "成功", resultMap);  
	}
		
	/**
	 * 意见反馈删除
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/delete",method = RequestMethod.POST)
	public JsonResult deleteFeedBack(HttpServletRequest request,String sid){
		
		int row =systemInfoService.deleteByPrimaryKey(sid);
		
		if(row>0)
		{
			return new JsonResult(true, "成功");  
		}else
		{
			return new JsonResult(false, "成功");  
		}
	}
		
}
